package ferp.android.activities;

import android.app.Dialog;
import android.app.TabActivity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TabHost;
import android.widget.TextView;
import ferp.android.GUI;
import ferp.android.R;
import ferp.android.activities.preferences.Preferences;
import ferp.android.dialogs.ProgressDialog;
import ferp.android.managers.ProfileManager;

/**
 * User: igorgok
 * Date: 5/29/12 5:10 PM
 */
public class Help extends TabActivity
{
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    pm = ProfileManager.regular(getApplication());

    // set correct orientation depends on if running on tablet or not
    GUI.setOrientation(this, pm.getCurrentProfile());

    super.onCreate(savedInstanceState);

    Preferences.setLocale(this, pm.getCurrentProfile());

    setContentView(R.layout.help);

    final String path = "file:///android_asset/help-" + getResources().getString(R.string.help_locale) + "/";

    host = getTabHost();

    // howto
    final WebView howto = setup(Tab.HOWTO, R.id.howto, R.string.help_howto, host);
    howto.loadUrl(path + "manual.html");

    // wiki
    final WebView wiki = setup(Tab.WIKI, R.id.wiki, R.string.help_wiki, host);

    wiki.setWebViewClient(new WebViewClient()
    {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url)
      {
        view.loadUrl(url);

        return true;
      }

      @Override
      public void onPageFinished(WebView view, String url)
      {
        if (!wikiLoaded)
        {
          GUI.tryToDismissDialog(Help.this, PROGRESS);
          wikiLoaded = true;
        }
      }

      @Override
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
      {
        if (!wikiLoaded)
          GUI.tryToDismissDialog(Help.this, PROGRESS);

        GUI.toast(Help.this, R.string.error_help);

        wiki.setVisibility(View.INVISIBLE);
      }
    });

    // about
    final WebView about = setup(Tab.ABOUT, R.id.about, R.string.help_about, host);
    about.loadUrl(path + "about.html");

    setupStyle();

    host.setOnTabChangedListener(new TabHost.OnTabChangeListener()
    {
      public void onTabChanged(String tag)
      {
        if (tag.equals(Tab.WIKI.toString()) && !wikiLoaded)
        {
          showDialog(PROGRESS);
          wiki.loadUrl(getResources().getString(R.string.help_url));
        }
      }
    });

    String tab = PreferenceManager.getDefaultSharedPreferences(this).getString(HELP_LAST_SHOWN_TAB, null);

    host.setCurrentTabByTag(tab == null ? Tab.HOWTO.toString() : tab);
  }

  @Override
  protected void onDestroy()
  {
    SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();

    editor.putString(HELP_LAST_SHOWN_TAB, host.getCurrentTabTag());
    editor.commit();

    super.onDestroy();
  }

  @Override
  protected Dialog onCreateDialog(int id)
  {
    switch (id)
    {
      case PROGRESS:
        ProgressDialog progress = new ProgressDialog(this);
        progress.set(R.string.please_wait, R.string.help_loading);
        progress.setCancelable(true);
        return progress;
    }

    return super.onCreateDialog(id);
  }

  @Override
  protected void onPrepareDialog(int id, Dialog dialog)
  {
    Preferences.setLocale(this, pm.getCurrentProfile());

    super.onPrepareDialog(id, dialog);
  }

  private static final int PROGRESS = 1;
  private static final String HELP_LAST_SHOWN_TAB = "helpLastShownTab";

  private static enum Tab {HOWTO, WIKI, ABOUT}

  private TabHost host;
  private boolean wikiLoaded = false;

  private ProfileManager pm;

  private WebView setup(Tab tab, int id, int indicator, TabHost th)
  {
    WebView v = (WebView)findViewById(id);
    WebSettings s = v.getSettings();

    //v.setBackgroundColor(Color.TRANSPARENT);
    s.setDefaultTextEncodingName("UTF-8");
    s.setJavaScriptEnabled(true);
    s.setJavaScriptCanOpenWindowsAutomatically(false);
    s.setSupportMultipleWindows(false);

    Drawable icon = null;
    switch (tab)
    {
      case HOWTO:
        icon = getResources().getDrawable(R.drawable.ic_menu_info_details);
        break;

      case WIKI:
        icon = getResources().getDrawable(R.drawable.logo_wikipedia);
        break;

      case ABOUT:
        icon = getResources().getDrawable(R.drawable.ic_menu_help);
        break;
    }

    th.addTab(th.newTabSpec(tab.toString()).setIndicator(getResources().getString(indicator), icon).setContent(id));

    return v;
  }

  private void setupStyle()
  {
    for(int i = 0; i < host.getTabWidget().getChildCount(); ++i)
    {
      TextView tv = (TextView)host.getTabWidget().getChildAt(i).findViewById(android.R.id.title);

      tv.setTypeface(GUI.Font.NORMAL);
      tv.setTextColor(Color.WHITE);
      tv.setShadowLayer(1, 1, 1, 0xFF000000);
    }
  }
}
